package cn.com.unionlife.hy.media.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import cn.com.unionlife.hy.core.controller.BaseController;
import cn.com.unionlife.hy.core.exception.BusinessException;
import cn.com.unionlife.hy.core.vo.PaginationVO;
import cn.com.unionlife.hy.media.service.MediaQryService;
import cn.com.unionlife.hy.media.service.MediaTranService;
import cn.com.unionlife.hy.media.vo.MediaConditionVO;
import cn.com.unionlife.hy.media.vo.MediaVO;

@Controller("meediaController")
public class MediaController extends BaseController {
	
	public final static String PHOTO_PATH = "/images/media/photo/";
	
	@Resource(name = "mediaQryService")
	private MediaQryService mediaQryService;
	
	@Resource(name = "mediaTranService")
	private MediaTranService mediaTranService;
	
	@RequestMapping(value = "/media/list")
	public String find(HttpServletRequest request ,MediaConditionVO condition, Model model) throws BusinessException{
		if(condition == null){
			condition = new MediaConditionVO();
		}
		condition.setUserId(getLoginedUser(request).getUm_user_id());
		PaginationVO<MediaVO> pagination = mediaQryService.findByCondition(condition);
		model.addAttribute(PAGINATION_KEY, pagination);
		return "/media/list";
	}
	
	@RequestMapping(value = "/media/add" , method = RequestMethod.GET)
	public String goAdd(HttpServletRequest request , Model model){
		model.addAttribute("status", "add");
		model.addAttribute(LOGINED_USER, getLoginedUser(request));
		model.addAttribute("media", new MediaVO());
		return "/media/add";
	}
	
	@RequestMapping(value = "/media/add" , method = RequestMethod.POST)
	public String doAdd(HttpServletRequest request , MediaVO media , Model model) throws BusinessException{
		if(media != null){
			media.setCreateUser(getLoginedUser(request));
			mediaTranService.addMedia(media);
		}
		return ajaxDone(200, "添加媒体成功", null, model);
	}
	
	@RequestMapping(value = "/media/uploadphoto" , method = RequestMethod.POST)
	public String doUploadPhoto(HttpServletRequest request , @RequestParam("photo") MultipartFile file , Model model) throws IllegalStateException, IOException{
		//照片目录位于{PHOTO_PATH}/yyyy/MM/dd/{UUID}下
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyy/MM/dd");       
        /**构建图片保存的目录**/      
        String photoPathDir = PHOTO_PATH + dateformat.format(new Date());       
        /**得到图片保存目录的真实路径**/      
        String photoRealPathDir = request.getSession().getServletContext().getRealPath(photoPathDir);
        
        String photoFileName = UUID.randomUUID().toString();
        File photoFile = new File(photoRealPathDir , photoFileName);
        photoFile.mkdirs();
		file.transferTo(photoFile);
		model.addAttribute("photoPath", photoPathDir + "/" + photoFileName);
		return "/media/uploadSuccess";
	}
	
	@RequestMapping(value = "/media/uploadphoto" , method=RequestMethod.GET)
	public String goUploadPhoto(){
		return "/media/uploadPhoto";
	}
	
	@RequestMapping(value = "/media/delete/{id}")
	public String delete(@PathVariable("id") Long id , Model model) throws BusinessException{
		if(id != null){
			mediaTranService.deleteMediaById(id);
		}
		return ajaxDone(200, "删除成功", null, model);
	}
	@RequestMapping(value = "/media/edit/{id}" , method = RequestMethod.GET)
	public String goEdit(@PathVariable("id")Long id , Model model) throws BusinessException{
		model.addAttribute("media", mediaQryService.getMediaById(id));
		return "/media/edit";
	}
	
	@RequestMapping(value = "/media/edit" , method = RequestMethod.POST)
	public String doEdit(HttpServletRequest request , MediaVO mediaVO , Model model) throws BusinessException{
		if(mediaVO != null){
			mediaTranService.updateMedia(mediaVO);
		}
		return ajaxDone(200, "编辑成功", null, model);
	}
	

}
